package com.yc.app1;

import java.util.Date;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/10/26
 * Email haijunzhou@hnit.edu.cn
 */
public class StringDemo {

    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = new String("abc");
        String s3 = new String( new char[]{'a', 'b', 'C'});

        System.out.println("(s1 == s2) = " + (s1 == s2));
        System.out.println("s1.equals(s2) = " + s1.equals(s2));
        System.out.println("s1.equals(s3) = " + s1.equals(s3));
        System.out.println("s1.equalsIgnoreCase(s3) = " + s1.equalsIgnoreCase(s3));
        // 排序 -1 小  0 相等  1 大
        System.out.println("s1.compareTo(s2) = " + s1.compareTo(s2));
        // 根据下标检索字符串中的字符
        System.out.println("s1.charAt(2) = " + s1.charAt(2));
        // 查找
        System.out.println("s1.indexOf(\"ab\") = " + s1.indexOf("ab"));
        System.out.println("\"abc, abc, abc, efg\".lastIndexOf(\"abc\") = " + "abc, abc, abc, efg".lastIndexOf("abc"));

        // 转换
        System.out.println("String.valueOf(1000) = " + String.valueOf(1000));
        System.out.println("String.valueOf(3.14) = " + String.valueOf(3.14));

        System.out.println("\"ABC\".toLowerCase() = " + "ABC".toLowerCase());
        System.out.println("\"xyz\".toUpperCase() = " + "xyz".toUpperCase());

        String s = String.format("你好 %s , %d 岁", "张三", 80);
        System.out.println("s = " + s);

        // 字符串的不变性
        String a = "abc";
        System.out.println("a.hashCode() = " + a.hashCode());
        System.out.println("a = " + a);
        a += "efg";
        System.out.println("a.hashCode() = " + a.hashCode());
        System.out.println("a = " + a);

        // 创建当前时间, 用于记录
        long begin = System.currentTimeMillis();
        String ss = "";
        for (int i = 0; i < 100000; i++) {
            ss += "a";
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗时 : " + (end - begin));

        begin = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < 100000; i++) {
            sb.append("a");
        }
        end = System.currentTimeMillis();
        System.out.println("共耗时 : " + (end - begin));

    }

}
